import { _decorator, Component, Node, BoxCollider, ITriggerEvent } from 'cc';
import { constant } from '../constant';
import { PoolManager } from '../framework/PoolManager';
const { ccclass, property } = _decorator;
const OUTOFRANGE = -50;
@ccclass('bullet')
export class Bullet extends Component {
    private bulletSpeed = 0;
    private _isEnemyBullet = false;
    private _direct = constant.Direction.MID;//子弹发射方向，默认mid
    onEnable() {
        const collider = this.getComponent(BoxCollider);
        collider.on("onTriggerEnter", this._onTriggerEnter, this);
    }
    onDisEnable() {
        const collider = this.getComponent(BoxCollider);
        collider.off("onTriggerEnter", this._onTriggerEnter, this);
    }
    start() {

    }

    update(deltaTime: number) {
        const pos = this.node.position;
        let moveLength = 0;
        if (this._isEnemyBullet) {
            moveLength = pos.z + this.bulletSpeed;
            this.node.setPosition(pos.x, pos.y, moveLength);
            if (moveLength > -OUTOFRANGE) {
                //this.node.destroy();
                PoolManager.instance().putNode(this.node);
                //console.log("PBisdestroy");
            }
        }
        else {
            moveLength = pos.z - this.bulletSpeed;
            if (this._direct == constant.Direction.LEFT) {
                this.node.setPosition(pos.x - this.bulletSpeed * 0.3, pos.y, moveLength);
            }
            else if (this._direct == constant.Direction.RIGHT) {
                this.node.setPosition(pos.x + this.bulletSpeed * 0.3, pos.y, moveLength);
            }
            else {
                this.node.setPosition(pos.x, pos.y, moveLength);
            }
            if (moveLength < OUTOFRANGE) {
                //this.node.destroy();
                //console.log("EBisdestroy");
                PoolManager.instance().putNode(this.node);
            }
        }
    }
    private _onTriggerEnter(event: ITriggerEvent) {
        //this.node.destroy();
        PoolManager.instance().putNode(this.node);
    }
    show(speed: number, _isEnemyBullet: boolean, direct: number) {
        this.bulletSpeed = speed;
        this._isEnemyBullet = _isEnemyBullet;
        this._direct = direct;
    }
}

